﻿// Defines an HTML wrapper element that can have children ones
define([
    'Wjs/Wjs'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'DomAttributes',

        internal: {

            construct: function () {

                this.attributes = {};
            },

            listeners: {

                onDomElementCreated: function () {

                    // Apply the attributes to the DOM
                    this.setAttributes(this.attributes);
                },

                onCloned: function (cloned) {

                    this.copyDomAttributesTo(cloned);
                }
            }

        },

        // Sets a single attribute
        setAttribute: function (name, value) {
            var attributes = this.attributes,
                oldValue = this.getAttribute(name);

            if (oldValue) { // There is already an attribute with that name

                Wjs.log('debug', 'Replacing attribute of name: ' + name + ' and value: ' + oldValue + ' with value: ' + value)
            }

            if (this.isRendered()) {

                this.domEl.setAttribute(name, value);
            }
            else { // Not rendered

                attributes[name] = value;
            }

            return this;
        },

        // Sets a group of attributes
        setAttributes: function (attributes) {
            var value;

            for (var name in attributes) {

                value = attributes[name];

                this.setAttribute(name, value);
            }

            return this;
        },

        // Gets an attribute value by its name
        getAttribute: function (name) {

            if (this.isRendered()) {

                return this.domEl.getAttribute(name);
            }
            else { // Not rendered

                return this.attributes[name];
            }
        },

        getAttributes: function (name) {

            if (this.isRendered()) {

                return this.domEl.attributes;
            }
            else { // Not rendered

                return this.attributes;
            }
        },

        // Tests whether the view has the attribute
        hasAttribute: function (name) {

            if (this.isRendered()) {

                return this.domEl.hasAttribute(name);
            }
            else { // Not rendered

                return Wjs.isDefined(this.attributes[name]);
            }
        },

        // Removes an attribute from the element
        removeAttribute: function (name) {

            if (this.isRendered()) {

                this.domEl.removeAttribute(name);
            }
            else {

                delete this.attributes[name];
            }
        },

        // Copies the attributes of this to the view
        copyDomAttributesTo: function (view) {

            view.setAttributes(this.getAttributes());
        }

    });

});